
$(function () {
	var popMenufuncid;

	var focusNum = 0; 
    /* 设置右侧历史信息滚动条 */
    $(".historyHeight").height($(window).height() - 50 + "px");
    $(".tab-pane").height($(".control-sidebar-halfheight").height() - 88 + "px");
    var timeoutflag = null;
    $(".historyTwinkle").click(function (){
    	if($(".control-sidebar.control-sidebar-open").length=="1"){
    	var alarmTypeCode="";
    	var deviceTypeId="";
		if($(".smokeCharts").hasClass("control-sidebar-open")){
			$(".smokeCharts").addClass("padding0");
		}else{
			$(".smokeCharts").removeClass("padding0");
		}
    	if(devHeader == "smokeHeader"){
    		$(".alarmhistorybtn i").removeClass("cover-alarm-tab humiture-alarm-tab").addClass("fire-alarm-tab");
    		alarmTypeCode = "001";
    		deviceTypeId = "001";
    		$(".lowhistorybtn").show();
    	}else if(devHeader == "manholeCoverHeader"){
    		$(".alarmhistorybtn i").removeClass("fire-alarm-tab humiture-alarm-tab").addClass("cover-alarm-tab");
    		deviceTypeId = "005";
    		alarmTypeCode = "010";
    		$(".lowhistorybtn").show();
    	}else if(devHeader == "humitureHeader"){
    		$(".alarmhistorybtn i").removeClass("fire-alarm-tab cover-alarm-tab").addClass("humiture-alarm-tab");
    		deviceTypeId = "007";
    		alarmTypeCode = "061";
    		$(".lowhistorybtn").hide();
    		
    	}
            if (timeoutflag != null) {
                clearTimeout(timeoutflag);
                timeoutflag = null;
            } else {
                timeoutflag = setTimeout(function () {
		    	$(".object-ul").html("");
		    	$('#control-sidebar-settings-tab').html("");
		    	$('#control-sidebar-home-tab').html("");
		    	$('#control-sidebar-theme-demo-options-tab').html("");
		    		//烟感历史消息的接口,定义url变量
		    		
		    	    $.ajax({
		    	        url: '/alarm/data/device/focusbycount',
		    	        type: 'GET',
		    	        async : false, //同步执行
		    	        success: function (result) {
		    	            if (result.status == 200) {
		    	            	moreThanNum = result.data;
		    	                return moreThanNum;
		    	            } else {
		    	            	winTip.alert(data.message)
		    	            }
		    	        }
		    	    });
//		    	    重点关注
		    	    console.log("/alarm/data/device/count?beginTime="+beginTime1+"&endTime="+endTime1+"&alarmTypeCode="+alarmTypeCode+"&deviceTypeId="+deviceTypeId)
		    	    $.ajax({
		    	        url: "/alarm/data/device/count?beginTime="+beginTime1+"&endTime="+endTime1+"&alarmTypeCode="+alarmTypeCode+"&deviceTypeId="+deviceTypeId,
		    	        type: 'GET',
		    	        async : false, //同步执行
		    	        success: function (result) {
		    	        	console.log(result)
		    	            if (result.status == 200) {
		    	                for (var i = 0; i < result.data.length; i++) {
		    	                    var data = result.data[i];
		    	                    focusNum = data[1];
		    	                    $(".sidebar-content-title >span").html('（'+result.data.length+'）');//— 近七天报警超过 '+moreThanNum+' 条的设备
		    	            	    $(".object-ul").append('<li latitude='+data[0].latitude+' longitude='+data[0].longitude+' devicecode='+data[0].devCode+'><span class="label label-primary">'+focusNum+'</span><span class="object-address">'+data[0].address+'</span></li>');
		    	            	}
		    	            } else {
		    	            	winTip.alert(result.message);
		    	            }
		    	        }
		    	    });
		    	    /* 报警历史信息记录 */
		    	    $.ajax({
		    	        url: "/alarm/data/history/simple/list?beginTime="+beginTime+"&endTime="+endTime+"&deviceTypeId="+devTypeId,
		    	        type: 'GET',
		    	        async : false,
		    	        success: function (result) {
		    	            if (result.status == 200) {
		    	                for (var i = 0; i < result.data.length; i++) {
		    	                    var data = result.data[i];
		    	                	var  devCode= data[0];
		    	                	var deviceName = data[1];
		    	                	var latitude = data[2];
		    	                	var longitude = data[3];
		    	                	var address = data[4];
		    	                	var alarmCode = data[5];
		    	                	var beginTime = moment(data[6]).format("YYYY-MM-DD HH:mm:ss");
		    	                    addHistoryNews(devCode,deviceName,latitude,longitude,address,alarmCode,beginTime);
		    	                    timeoutflag = null;
		    	                }
		    	            } else {
		    	            	winTip.alert(result.message)
		    	            }
		    	        }
		    	    });
		    	    
                },0);
                var timeoutflag = null;
            }
    	}
    });
    
    /* 点击历史消息定位到地图 */
    $(".historyList").on('click', '.li_device', function () {
        var li = $(this);
        if(li.hasClass("notclick"))
        	return;
        var devicecode = li.attr("devicecode");
        var latitude = li.attr("latitude");
        var longitude = li.attr("longitude");
        var statusAbbr = li.attr("statusAbbr");
        map.setZoom(20); 
        $('.BMap_pop').parent().show();
    	$(".BMap_shadow").parent().show();
    	
//        clearTimeout(popMenufuncid);
        BaiDuMap.panTo(latitude, longitude);
        $(".active-alarm-pointer .pulse").remove();
        $(".active-alarm-pointer .pulse1").remove();
//        BaiDuMap.popMenufuncid = setTimeout(function(){
        BaiDuMap.popMenu(devicecode);
//    	},500);
        
    });

    /*重点关注对象*/
//    focusAttention();
  //点击重点关注对象定位地图
    $(".object-ul").on('click', 'li', function (e) {
        var li = $(this);
        var devicecode = li.attr("devicecode");
        var latitude = li.attr("latitude");
        var longitude = li.attr("longitude");
        map.setZoom(20);
        BaiDuMap.panTo(latitude, longitude);
        $(".active-alarm-pointer .pulse").remove();
        $(".active-alarm-pointer .pulse1").remove();
//        地图信息框显示
        $('.BMap_pop').parent().show();
    	$(".BMap_shadow").parent().show();
    	BaiDuMap.popMenu(devicecode);
    });
    
  }); 

/*设置重点关注对象超过的条数*/
var moreThanNum = 0;
var beginTime1 = moment().add('days',-6).format('YYYY-MM-DD')+" 00:00:00";
var endTime1 = moment().add('days',0).format('YYYY-MM-DD')+" 23:59:59";

/*实时添加重点关注对象数量*/
function focusNumFun (devCode) {
	var focusNumber;
	var devicecode;
	
	$(".object-ul>li").each(function () {
		
		devicecode = $(this).attr("devicecode");
		if(devicecode == devCode){
			
			focusNumber = $(this).find(".label-primary").text();
			focusNumber++;
			$(this).find(".label-primary").text(focusNumber);
		}
	});
}

 // 历史消息实时封装添加
function historyInfo(deviceNameAbbr, addressAbbr, returnTimesAbbr, latitudeAbbr, longitudeAbbr, devCodeAbbr, statusAbbr) {
    var hisHtml = '<div class="form-group li_device mb5" devicecode="' + devCodeAbbr + '"' +' latitude = "' + latitudeAbbr + '"' +' longitude = "' + longitudeAbbr + '"' + ' statusAbbr = "' + statusAbbr + '"><label class="control-sidebar-subheading"><i class="fa fa-map-marker"></i><span>' + deviceNameAbbr + '</span></label><p class="mb3 fs12">' + addressAbbr + '</p><p class="historyTime mb5">' + returnTimesAbbr + '</p></div>';
   /* 历史消息按钮实时闪烁 */
    $('.smokeHeader .historyBgc').on('click', function () {
        $('.smokeHeader .historyBgc .historyMsg').removeAttr("id", "twinkle");
    });
 	$('.smokeHeader .historyBgc .historyMsg').attr("id", "twinkle");
   	/* 添加历史状态消息 */        
    if (statusAbbr == 1) {
        /* 火警 */
    	return $('#control-sidebar-theme-demo-options-tab').prepend(hisHtml);
    } else if (statusAbbr == 3) {
        /* 故障  */
    	return $('#control-sidebar-home-tab').prepend(hisHtml);
    } else if (statusAbbr == 4) {
        /* 低电 */
    	return $('#control-sidebar-settings-tab').prepend(hisHtml);
    }
};
// 历史消息实时封装添加
function coverHistoryInfo(deviceNameAbbr, addressAbbr, returnTimesAbbr, latitudeAbbr, longitudeAbbr, devCodeAbbr, statusAbbr) {
    var hisHtml = '<div class="form-group li_device mb5" devicecode="' + devCodeAbbr + '"' +' latitude = "' + latitudeAbbr + '"' +' longitude = "' + longitudeAbbr + '"' + ' statusAbbr = "' + statusAbbr + '"><label class="control-sidebar-subheading"><i class="fa fa-map-marker"></i><span>' + deviceNameAbbr + '</span></label><p class="mb3 fs12">' + addressAbbr + '</p><p class="historyTime mb5">' + returnTimesAbbr + '</p></div>';
   /* 历史消息按钮实时闪烁 */
    $('.smokeHeader .historyBgc').on('click', function () {
        $('.smokeHeader .historyBgc .historyMsg').removeAttr("id", "twinkle");
    });
 	$('.smokeHeader .historyBgc .historyMsg').attr("id", "twinkle");
   	/* 添加历史状态消息 */        
    if (statusAbbr == 10) {
        /* 火警 */
    	return $('#control-sidebar-theme-demo-options-tab').prepend(hisHtml);
    } else if (statusAbbr == 14) {
        /* 故障  */
    	return $('#control-sidebar-home-tab').prepend(hisHtml);
    } else if (statusAbbr == 12) {
        /* 低电 */
    	return $('#control-sidebar-settings-tab').prepend(hisHtml);
    }
};
// 历史消息实时封装添加
function humitureHistoryInfo(deviceNameAbbr, addressAbbr, returnTimesAbbr, latitudeAbbr, longitudeAbbr, devCodeAbbr, statusAbbr) {
    var hisHtml = '<div class="form-group li_device mb5" devicecode="' + devCodeAbbr + '"' +' latitude = "' + latitudeAbbr + '"' +' longitude = "' + longitudeAbbr + '"' + ' statusAbbr = "' + statusAbbr + '"><label class="control-sidebar-subheading"><i class="fa fa-map-marker"></i><span>' + deviceNameAbbr + '</span></label><p class="mb3 fs12">' + addressAbbr + '</p><p class="historyTime mb5">' + returnTimesAbbr + '</p></div>';
   /* 历史消息按钮实时闪烁 */
    $('.smokeHeader .historyBgc').on('click', function () {
        $('.smokeHeader .historyBgc .historyMsg').removeAttr("id", "twinkle");
    });
 	$('.smokeHeader .historyBgc .historyMsg').attr("id", "twinkle");
   	/* 添加历史状态消息 */        
    if (statusAbbr == 61) {
        /* 火警 */
    	return $('#control-sidebar-theme-demo-options-tab').prepend(hisHtml);
    } else if (statusAbbr == 62) {
        /* 故障  */
    	return $('#control-sidebar-home-tab').prepend(hisHtml);
    } 
//    else if (statusAbbr == 12) {
//        /* 低电 */
//    	return $('#control-sidebar-settings-tab').prepend(hisHtml);
//    }
};
/* 历史消息封装添加 */
function addHistoryNews(devCode,deviceName,latitude,longitude,address,alarmCode,beginTime) {
//    var address = datas.device.address;
//    var beginTime = moment(datas.beginTime).format("YYYY-MM-DD HH:mm:ss");
//    var deviceName = datas.device.deviceName;
	
    var alarmHisHtml = '<div class="form-group li_device mb5" devicecode="' + devCode + '"'+ ' latitude = "' + latitude + '"'+ ' longitude = "' + longitude + '"'+ ' statusAbbr = "' + alarmCode + '"'+ '><label class="control-sidebar-subheading"><i class="fa fa-map-marker"></i><span>' + deviceName + '</span></label><p class="mb3 fs12">' + address + '</p><p class="historyTime mb5">' + beginTime + '</p></div>'
    if (alarmCode == "001") {
    	/* 火警 */
        return $('#control-sidebar-theme-demo-options-tab').prepend(alarmHisHtml);
    } else if (alarmCode == "003") {
    	/* 故障  */
        return $('#control-sidebar-home-tab').prepend(alarmHisHtml);
    } else if (alarmCode == "002") {
        /* 低电 */
        return $('#control-sidebar-settings-tab').prepend(alarmHisHtml);
    } else if (alarmCode == "010") {
        /* 低电 */
        return $('#control-sidebar-theme-demo-options-tab').prepend(alarmHisHtml);
    } else if (alarmCode == "061") {
        /* 温湿度报警 */
        return $('#control-sidebar-theme-demo-options-tab').prepend(alarmHisHtml);
    } else if (alarmCode == "062") {
        /* 温湿度故障 */
        return $('#control-sidebar-home-tab').prepend(alarmHisHtml);
    } else {
        console.log('报警状态：' + alarmCode)
    }
};



